package org.nordligulv;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by IntelliJ IDEA.
 * User: nordligulv
 * Date: 15.02.11
 * Time: 0:25
 */
public class BeelineBalanceService {
    private static final String FORM_DATA = "userName=%s&password=%s&_stateParam=eCareLocale.currentLocale%%3Dru_RU__Russian&_forwardName=null&_resetBreadCrumbs=false&_expandStatus=&ecareAction=login";

    public String get(String login, String password) throws IOException {
        DefaultHttpClient client = new DefaultHttpClient();
        int proxyPort = android.net.Proxy.getDefaultPort();
        if (proxyPort > 0) {
            String proxyHost = android.net.Proxy.getDefaultHost();
            HttpHost proxy = new HttpHost(proxyHost, proxyPort);
            client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        }
        HttpPost post = new HttpPost("https://uslugi.beeline.ru/loginPage.do");
        post.setHeader("Content-Type", "application/x-www-form-urlencoded");
        HttpEntity entity = new StringEntity(String.format(FORM_DATA, login, password));
        post.setEntity(entity);

        HttpResponse response = client.execute(post);
        StringBuilder cookie = new StringBuilder();
        for (Cookie c : client.getCookieStore().getCookies()) {
            cookie.append(c.getName()).append('=').append(c.getValue()).append(';');
        }
        response.getEntity().consumeContent();

        HttpGet get = new HttpGet("https://uslugi.beeline.ru/vip/prepaid/refreshedPrepaidBalance.jsp");
        get.setHeader("cookie", cookie.toString());
//        client.setRedirectHandler(new RedirectHandler() {
//            @Override
//            public boolean isRedirectRequested(HttpResponse response, HttpContext context) {
//                return false;
//            }
//
//            @Override
//            public URI getLocationURI(HttpResponse response, HttpContext context) throws ProtocolException {
//                return null;
//            }
//        });
        response = client.execute(get);
        entity = response.getEntity();
        BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
        boolean priceLine = false;
        String balance = "-";
        String line;
        while ((line = reader.readLine()) != null) {
            if (priceLine) {
                line = line.trim();
                int i = line.indexOf("&nbsp;");
                balance = line.substring(0, i);
                break;
            }
            if (line.contains("tabred")) {
                priceLine = true;
            }
        }

        return balance;
    }
}
